﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class User_SearchExpert : System.Web.UI.Page
{
    ProcessUser processUser;
    public void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            processUser = new ProcessUser();
            fillCbCat();
        }
    }

    public void fillCbCat ()
    {
        try
        {
            processUser.selectRootCategory();
            cbCat1.DataSource = ProcessUser.dataset.Tables["RootCategory"];
            cbCat1.ValueField = "CatID";
            cbCat1.TextField = "CatName"; 
            cbCat1.DataBind();
            cbCat1.Items.Add("Tất cả").Index =0;
            cbCat1.SelectedIndex = 0;       
            
        }
        catch (Exception ex)
        {
            Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
        }
    }
    
    public void btnReset_Click(object sender, EventArgs e)
    {
        txtName.Text = "";
        txtDes.Text = "";
        txtExperience.Text = "";
        txtPriceChat_From.Text = "";
        txtPriceChat_To.Text = "";
        txtPriceEmail_From.Text = "";
        txtPriceEmail_to.Text = "";
        txtKnowledge.Text = "";
        cbCat1.SelectedIndex = 0;
        cbCat2.SelectedIndex = -1;
        cbCat2.Enabled = false;
        cbCat3.SelectedIndex = -1;
        cbCat3.Enabled = false;
        chkOnline.Checked = false;
        chkRating.Checked = false;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        try
        {
            string catID = "";
            if (cbCat1.SelectedIndex == 0)
            {
                catID = "";
            }
            else if (cbCat2.SelectedIndex == 0)
            {
                catID = cbCat1.SelectedItem.Value.ToString();
            }
            else if (cbCat3.SelectedIndex == 0)
            {
                catID = cbCat2.SelectedItem.Value.ToString();
            }
            else
            {
                catID = cbCat3.SelectedItem.Value.ToString();
            }

            string rate = "";
            if (cbRating.SelectedItem.Value.ToString() == "0")
            {
                rate = "";
            }
            else
            {
                rate = cbRating.SelectedItem.ToString();
            }
            string name = txtName.Text.ToString();
            string des = txtDes.Text.ToString();
            string experience = txtExperience.Text.ToString();
            string knowledge = txtKnowledge.Text.ToString();
            string emailPrice_to = txtPriceEmail_to.Text.ToString();
            string emailPrice_from = txtPriceEmail_From.Text.ToString();
            string chatPrice_to = txtPriceChat_To.Text.ToString();
            string chatPrice_from = txtPriceEmail_From.Text.ToString();
            bool online = chkOnline.Checked;
            bool rating = chkRating.Checked;

            SearchProcess searchProcess = new SearchProcess();
            string datafile = searchProcess.search_expert_advance(name, des, knowledge, experience, chatPrice_to, chatPrice_from, emailPrice_to, emailPrice_from, rating, rate, online, catID);
            Response.Redirect("ShowExpert.aspx?file="+datafile, false);
        }
        catch (Exception ex)
        {
            Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
        }
    }

    protected void cbCat1_SelectedIndexChanged(object sender, EventArgs e)
    {
     //  Label20.Text = cbCat1.SelectedIndex.ToString();
        if (cbCat1.SelectedIndex == 0)
        {
            cbCat2.SelectedIndex = -1;
            cbCat2.Enabled = false;
            cbCat3.SelectedIndex = -1;
            cbCat3.Enabled = false;
        }
        else
        {
            try
            {
                    cbCat2.Enabled = true;
                    processUser = new ProcessUser();
                    processUser.selectChildCategory(cbCat1.SelectedItem.Value.ToString(), "ChildCategory1");
                    //Label20.Text = cbCat1.SelectedItem.Value.ToString();
                    cbCat2.DataSource = ProcessUser.dataset.Tables["ChildCategory1"];
                    cbCat2.TextField = "CatName";
                    cbCat2.ValueField = "CatID";
                    cbCat2.DataBind();
                    cbCat2.Items.Add("Tất cả").Index = 0;
                    cbCat2.SelectedIndex = 0;
                
            }
            catch (Exception ex)
            {
                HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
            }
        }
   
    }
        
    protected void cbCat2_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            if (cbCat2.SelectedIndex == 0)
            {
                cbCat3.SelectedIndex = -1;
                cbCat3.Enabled = false;
            }
            else
            {
                processUser = new ProcessUser();
                processUser.selectChildCategory(cbCat2.SelectedItem.Value.ToString(), "ChildCategory2");
              //  Label19.Text = cbCat2.SelectedItem.Value.ToString();
                cbCat3.Enabled = true;
                cbCat3.DataSource = ProcessUser.dataset.Tables["ChildCategory2"];
                cbCat3.TextField = "CatName";
                cbCat3.ValueField = "CatID";
                cbCat3.DataBind();
                cbCat3.Items.Add("Tất cả").Index =0 ;
                cbCat3.SelectedIndex = 0;
            }
        }
        catch (Exception ex)
        {
            HttpContext.Current.Response.Redirect("UserThongbao.aspx?id=" + HttpUtility.UrlEncode(ex.ToString()));
        }
    
    }
 
}
